
use anyhow::Result;
use structopt::StructOpt;

use crate::cmd::Output;
use crate::cmd::SectorInput;

mod pledge;
mod fix;
mod ticket;

#[derive(StructOpt, Debug)]
pub enum SectorCommand {
    #[structopt(name = "pledge", about = "pledge sector to workers")]
    Pledge(Pledge),
    #[structopt(name = "data-fix", about = "fix sector data in database")]
    DataFix(DataFix),
    #[structopt(name = "ticket", about = "get sector ticket from chain")]
    Ticket(SectorInput),
}

#[derive(StructOpt, Debug)]
pub struct Pledge {
    #[structopt(long)]
    pub run: bool,
}


#[derive(StructOpt, Debug)]
pub struct DataFix {
    #[structopt(long)]
    pub miner: String,
}

impl SectorCommand {
    pub async fn run(self) -> Result<Option<Box<dyn Output>>> {
        match self {
            SectorCommand::Pledge(p) => p.run().await,
            SectorCommand::DataFix(f) => f.run().await,
            SectorCommand::Ticket(o) =>{
                ticket::find_sector_ticket(&o).await
            },
        }
    }
}

impl Pledge {
    pub async fn run(self) -> Result<Option<Box<dyn Output>>> {
        pledge::pledge_sector(self.run).await
    }
}

impl DataFix {
    pub async fn run(self) -> Result<Option<Box<dyn Output>>> {
        fix::fix_sector_data(self.miner.as_str()).await
    }
}